package com.techsoul.common.core.manager.factory;

import com.techsoul.common.core.constant.ApiConstants;
import com.techsoul.common.core.pojo.param.LoginLogSaveParam;
import com.techsoul.common.core.pojo.param.OperationLogSaveParam;
import com.techsoul.common.core.service.ILoginLogService;
import com.techsoul.common.core.service.IOperationLogService;
import com.techsoul.common.core.util.ServletUtils;
import com.techsoul.common.core.util.lang.StringUtils;
import com.techsoul.common.core.util.network.AddressUtils;
import com.techsoul.common.core.util.network.IpUtils;
import com.techsoul.common.core.util.spring.SpringUtils;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.extern.slf4j.Slf4j;

import java.util.TimerTask;

/**
 * 异步工厂（产生任务用）
 *
 * @author liuen
 */
@Slf4j(topic = "sys-user")
public class AsyncFactory {

    /**
     * 记录登录信息
     *
     * @param username 用户名
     * @param status   状态
     * @param message  消息
     * @param args     列表
     * @return 任务task
     */
    public static TimerTask recordLoginLog(final String username, final String status, final String message, final Object... args) {
        final UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
        final String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
        return new TimerTask() {
            @Override
            public void run() {
                String address = AddressUtils.getRealAddressByIP(ip);
                StringBuilder s = new StringBuilder();
                s.append(StringUtils.getBlock(ip));
                s.append(address);
                s.append(StringUtils.getBlock(username));
                s.append(StringUtils.getBlock(status));
                s.append(StringUtils.getBlock(message));
                // 打印信息到日志
                log.info(s.toString(), args);
                // 获取客户端操作系统
                String os = userAgent.getOperatingSystem().getName();
                // 获取客户端浏览器
                String browser = userAgent.getBrowser().getName();
                // 封装对象
                LoginLogSaveParam loginLogSaveParam = new LoginLogSaveParam();
                loginLogSaveParam.setUserName(username);
                loginLogSaveParam.setIp(ip);
                loginLogSaveParam.setLocation(address);
                loginLogSaveParam.setBrowser(browser);
                loginLogSaveParam.setOs(os);
                loginLogSaveParam.setMsg(message);
                // 日志状态
                if (ApiConstants.LOGIN_SUCCESS.equals(status) || ApiConstants.LOGOUT.equals(status)) {
                    loginLogSaveParam.setStatus(ApiConstants.SUCCESS);
                } else if (ApiConstants.LOGIN_FAIL.equals(status)) {
                    loginLogSaveParam.setStatus(ApiConstants.FAIL);
                }
                loginLogSaveParam.setCreateBy(ApiConstants.DEFAULT_USER_ID);
                loginLogSaveParam.setUpdateBy(ApiConstants.DEFAULT_USER_ID);
                // 插入数据
                SpringUtils.getBean(ILoginLogService.class).insert(loginLogSaveParam);
            }
        };
    }

    /**
     * 操作日志记录
     *
     * @param operLog 操作日志信息
     * @return 任务task
     */
    public static TimerTask recordOperationLog(final OperationLogSaveParam operLog) {
        return new TimerTask() {
            @Override
            public void run() {
                // 远程查询操作地点
                operLog.setLocation(AddressUtils.getRealAddressByIP(operLog.getIp()));
                SpringUtils.getBean(IOperationLogService.class).insert(operLog);
            }
        };
    }
}
